package com.tutu.controller.fragment;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.hyphenate.easeui.ui.EaseContactListFragment;
import com.tutu.R;
import com.tutu.constant.ContactConst;
import com.tutu.controller.activity.AddContcatActivity;
import com.tutu.controller.activity.InviteActivity;
import com.tutu.utils.SpUtils;

/**
 * @author 图图 2020/10/11
 * 联系人列表页面
 */
public class ContactFragment extends EaseContactListFragment {

    private ImageView iv_contact_red;
    private LinearLayout ll_contact_invite;

    private LocalBroadcastManager mLBM;

    private BroadcastReceiver contactInviteReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 更新红点显示
            iv_contact_red.setVisibility(View.VISIBLE);
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
        }
    };



    @Override
    protected void initView() {
        super.initView();
        // 右侧显示加号
        titleBar.setRightImageResource(R.mipmap.add);

        // 添加头布局
        View headerView = View.inflate(getActivity(), R.layout.header_fragment_contact, null);
        listView.addHeaderView(headerView);

        // 获取新邀请”红点“对象
        iv_contact_red = (ImageView)headerView.findViewById(R.id.iv_contact_red);

        // "新朋友"条目对象
        ll_contact_invite = (LinearLayout)headerView.findViewById(R.id.ll_contact_invite);

    }


    @Override
    protected void setUpView() {
        super.setUpView();
        // 添加按钮的点击事件
        titleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到添加联系人页面
                openNewPageNoFinish(getActivity(), AddContcatActivity.class);
            }
        });

        // 初始化红点状态
        boolean isNewInvite = SpUtils.getInstance().getBoolean(SpUtils.IS_NEW_INVITE, false);
        iv_contact_red.setVisibility(isNewInvite ? View.VISIBLE : View.GONE);

        // "新朋友"条目点击事件
        ll_contact_invite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消红点显示
                iv_contact_red.setVisibility(View.GONE);
                SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,false);
                // 跳转邀请信息列表页面
                openNewPageNoFinish(getActivity(), InviteActivity.class);
            }
        });

        // 注册广播
        mLBM = LocalBroadcastManager.getInstance(getActivity());
        mLBM.registerReceiver(contactInviteReceiver,new IntentFilter(ContactConst.CONTACT_INVITE_CHANGED));


    }

    @Override
    public  void  onDestroy(){

        super.onDestroy();
        // 关闭广播
        mLBM.unregisterReceiver(contactInviteReceiver);
    }



    /**
     * 跳转
     * @param packageContext
     * @param cls
     */
    private void  openNewPage(Context packageContext, Class<?> cls){
        // 页面跳转
        Intent intent =  new Intent(packageContext,cls);
        // 启动跳转
        startActivity(intent);
        // 结束当前页
        getActivity().finish();
    }

    /**
     * 跳转
     * @param packageContext
     * @param cls
     */
    private void  openNewPageNoFinish(Context packageContext, Class<?> cls){
        // 页面跳转
        Intent intent =  new Intent(packageContext,cls);
        // 启动跳转
        startActivity(intent);
    }
}
